package com.hr.异步调用;

import lombok.extern.slf4j.Slf4j;

/**
 * @program: leetcode
 * @description:
 * @author: HuRan QQ1345122890
 * @create: 2021-08-11 18:14
 */
@Slf4j(topic = "线程中断2阶段")
public class Demo04 {
    public static void main(String[] args) throws InterruptedException {
        TowTermi t=new TowTermi();
        t.start();
        Thread.sleep(3000);
        t.stop();
    }
    static class TowTermi {
        private Thread monitor;

        public void start() {
            monitor = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        final Thread thread = Thread.currentThread();

                        if (thread.isInterrupted()) {
                            log.debug("处理后续。。。");
                            break;
                        }
                        try {
                            Thread.sleep(1000);
                            log.debug("执行监控。。。。");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            //重新设置中断标记
                            thread.interrupt();
                        }
                    }
                }
            });
            monitor.start();
        }

        public void stop() {
            monitor.interrupt();
        }
    }
}